package com.jiuyou.clientController.instrument;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.instrument.domain.Instrument;
import com.ruoyi.instrument.service.IInstrumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 仪器信息Controller
 *
 * @author ruoyi
 * @date 2025-05-07
 */
@RestController
@RequestMapping("/client/instrument")
public class ClientInstrumentController extends BaseController
{
    @Autowired
    private IInstrumentService instrumentService;

    /**
     * 查询仪器信息列表(所有)
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(Instrument instrument)
    {
        List<Instrument> list = instrumentService.selectInstrumentList(instrument);
        return AjaxResult.success(list);
    }

    /**
     * 查询仪器信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Instrument instrument)
    {
        startPage();
        List<Instrument> list = instrumentService.selectInstrumentList(instrument);
        return getDataTable(list);
    }

    /**
     * 查询仪器使用记录列表
     */
    @GetMapping("/instrumentRecordList")
    public TableDataInfo instrumentRecordList(Instrument instrument)
    {
        startPage();
        List<Map<String,Object>> list = instrumentService.selectInstrumentRecordList(instrument);
        return getDataTable(list);
    }

    /**
     * 导出仪器信息列表
     */
    @Log(title = "仪器信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Instrument instrument)
    {
        List<Instrument> list = instrumentService.selectInstrumentList(instrument);
        ExcelUtil<Instrument> util = new ExcelUtil<Instrument>(Instrument.class);
        return util.exportExcel(list, "仪器信息数据");
    }

    /**
     * 获取仪器信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(instrumentService.selectInstrumentById(id));
    }

    /**
     * 新增仪器信息
     */
    @Log(title = "仪器信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Instrument instrument)
    {
        return toAjax(instrumentService.insertInstrument(instrument));
    }

    /**
     * 新增仪器使用记录信息
     */
    @Log(title = "仪器信息", businessType = BusinessType.INSERT)
    @PostMapping("/addRecord")
    public AjaxResult addRecord(@RequestBody Instrument instrument)
    {
        return toAjax(instrumentService.insertInstrumentRecord(instrument));
    }

    /**
     * 修改仪器信息
     */
    @Log(title = "仪器信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Instrument instrument)
    {
        instrument.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(instrumentService.updateInstrument(instrument));
    }

    /**
     * 删除仪器信息
     */
    @Log(title = "仪器信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(instrumentService.logicDeleteInstrument(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }
}
